package su.samcom.calc.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import su.samcom.calc.service.util.CalculatorUtil;
import su.samcom.calc.exception.RouteNotFoundException;
import su.samcom.calc.model.entity.Price;
import su.samcom.calc.model.entity.Property;
import su.samcom.calc.service.CalculatorService;
import su.samcom.calc.service.CalculatorServiceImpl;
import su.samcom.calc.service.PropertyService;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 * User: Владислав
 * Date: 13.11.13
 * Time: 0:00
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class IndexController {

    @Autowired
    PropertyService propertyService;

    @Autowired
    CalculatorService calculatorService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(Model model){
        return "forward:/calculator";
    }

    @RequestMapping(value = "/properties")
    public @ResponseBody String properties(Model model){
        InputStream inputStream = IndexController.class.getResourceAsStream("classpath:*.properties");
        Properties properties = new Properties();
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return "Properties size: " + properties.size();
    }

    @RequestMapping(value = "/table")
    public String table(Model model){
        return "table";
    }

    @RequestMapping(value = "/chart")
    public String chart(Model model){
        return "chart";
    }

    @RequestMapping(value = "/property")
    public @ResponseBody String propertyAll(Model model){
        List<Property> properties = propertyService.getAll();
        String str = "";
        for(Property property : properties){
            str = str + String.format("%s : %s</br>", property.getKey(), property.getValue());
        }
        return str;
    }

    @RequestMapping(value = "/prices")
    public @ResponseBody String test(Model model){
        List<Price> prices = null;
        try {
            prices = calculatorService.getCargoPrices(50l, 60l, CalculatorServiceImpl.CargoType.SEPARATE);
        } catch (RouteNotFoundException e) {
            e.printStackTrace();
        }
        String str = "";
        for(Price price : prices){
            str = str + String.format("%s : %s</br>", CalculatorUtil.calculateLoadedPercent(price), price.getPrice());
        }
        return str;
    }

    @RequestMapping(value = "/price/{loadedPercent}")
    public @ResponseBody String calculatePrice(@PathVariable Double loadedPercent, Model model){
        List<Price> prices = null;
        try {
            prices = calculatorService.getCargoPrices(50l, 60l, CalculatorServiceImpl.CargoType.SEPARATE);
        } catch (RouteNotFoundException e) {
            e.printStackTrace();
        }
        int price = CalculatorUtil.calculatePrice(prices, loadedPercent);
        return String.format("%1.2f = %drub.", loadedPercent, price);
    }

    @RequestMapping(value = "/init-database")
    public @ResponseBody String initDatabase(){
        propertyService.initDatabase();
        return "Database inited";
    }

    @RequestMapping(value = "/table/json", method = RequestMethod.GET)
    public @ResponseBody String tableJsonGet(Model model){
        //return "{\"page\":\"1\",\"total\":2,\"records\":\"13\",\"rows\":[{\"id\":\"13\",\"cell\":[\"13\",\"2007-10-06\",\"Client 3\",\"1000.00\",\"0.00\",\"1000.00\",null]},{\"id\":\"12\",\"cell\":[\"12\",\"2007-10-06\",\"Client 2\",\"700.00\",\"140.00\",\"840.00\",null]},{\"id\":\"11\",\"cell\":[\"11\",\"2007-10-06\",\"Client 1\",\"600.00\",\"120.00\",\"720.00\",null]},{\"id\":\"10\",\"cell\":[\"10\",\"2007-10-06\",\"Client 2\",\"100.00\",\"20.00\",\"120.00\",null]},{\"id\":\"9\",\"cell\":[\"9\",\"2007-10-06\",\"Client 1\",\"200.00\",\"40.00\",\"240.00\",null]},{\"id\":\"8\",\"cell\":[\"8\",\"2007-10-06\",\"Client 3\",\"200.00\",\"0.00\",\"200.00\",null]},{\"id\":\"7\",\"cell\":[\"7\",\"2007-10-05\",\"Client 2\",\"120.00\",\"12.00\",\"134.00\",null]},{\"id\":\"6\",\"cell\":[\"6\",\"2007-10-05\",\"Client 1\",\"50.00\",\"10.00\",\"60.00\",\"\"]},{\"id\":\"5\",\"cell\":[\"5\",\"2007-10-05\",\"Client 3\",\"100.00\",\"0.00\",\"100.00\",\"no tax at all\"]},{\"id\":\"4\",\"cell\":[\"4\",\"2007-10-04\",\"Client 3\",\"150.00\",\"0.00\",\"150.00\",\"no tax\"]}],\"userdata\":{\"amount\":3220,\"tax\":342,\"total\":3564,\"name\":\"Totals:\"}}";
        return "{\"page\":\"1\",\"total\":1,\"records\":\"1\",\"rows\":[{\"id\":\"4\",\"cell\":{\"id\":\"4\",\"invdate\":\"2007-10-04\",\"name\":\"Client 3\",\"amount\":\"150.00\",\"tax\":\"0.00\",\"total\":\"150.00\",\"note\":\"no tax\"}}],\"userdata\":{\"amount\":3220,\"tax\":342,\"total\":3564,\"name\":\"Totals:\"}}";
    }

    @RequestMapping(value = "/table/json", method = RequestMethod.POST)
    public @ResponseBody String tableJsonPost(@RequestParam Long id,
                                              @RequestParam String oper,
                                              @RequestParam(required = false) Double amount,
                                              @RequestParam(required = false) Double tax,
                                              @RequestParam(required = false) Double total,
                                              @RequestParam(required = false) String note,
                                              Model model){
        return "";
    }

    @RequestMapping(value = "/chart/json")
    public @ResponseBody Object[] chartJsonPost(Model model){
        //return "[[[0.0, 1.7], [2.0, 5.7], [4.8, 3.0], [11.0, 5.1],[12.5, 21.9]]]";

        /*Object[] chart = new Object[5];
        chart[0] = new Double[]{0.0, 1.7};
        chart[1] = new Double[]{2.0, 5.7};
        chart[2] = new Double[]{4.8, 3.0};
        chart[3] = new Double[]{11.0, 5.1};
        chart[4] = new Double[]{12.5, 21.9};
        return new Object[]{chart};*/

        /*Double[][][] charts = new Double[1][5][2];
        charts[0][0][0] = 0.0; charts[0][0][1] =  1.7;
        charts[0][1][0] = 2.0; charts[0][1][1] =  5.7;
        charts[0][2][0] = 4.8; charts[0][2][1] = 3.0;
        charts[0][3][0] = 11.0; charts[0][3][1] =  5.1;
        charts[0][4][0] = 12.5; charts[0][4][1] = 21.9;
        return charts;*/

        /*PricePoint pricePoint = new PricePoint();
        pricePoint.setId(1l);
        pricePoint.setName("price point");
        pricePoint.setPrice(2000);
        return pricePoint;*/

        List<List> charts = new ArrayList<List>();
        List<List> chart = new ArrayList<List>();
        List<Double> p1 = new ArrayList<Double>();
        p1.add(0.0);
        p1.add(1.7);
        List<Double> p2 = new ArrayList<Double>();
        p2.add(2.0);
        p2.add(5.7);
        List<Double> p3 = new ArrayList<Double>();
        p3.add(4.8);
        p3.add(3.0);
        chart.add(p1);
        chart.add(p2);
        chart.add(p3);
        charts.add(chart);
        return charts.toArray();
    }
}
